package com.ruoyi.vehicles.controller;

import java.util.List;
import java.io.IOException;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.domain.SysVehicles;
import com.ruoyi.system.api.domain.UpdateUseUnitDTO;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.vehicles.form.AddVehiclesForm;
import com.ruoyi.vehicles.form.UpdateVehiclesForm;
import com.ruoyi.vehicles.param.AddVehiclesParam;
import com.ruoyi.vehicles.param.UpdateVehiclesParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.vehicles.domain.AVehicles;
import com.ruoyi.vehicles.service.IAVehiclesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 车辆Controller
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/vehicles")
public class AVehiclesController extends BaseController
{
    @Autowired
    private IAVehiclesService aVehiclesService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询该单位下的车辆列表
     */
    @RequiresPermissions("vehicles:vehicles:list")
    @GetMapping("/list/{id}")
    public TableDataInfo list(@PathVariable("id") Long id)
    {
        startPage();
        AVehicles aVehicles = new AVehicles();
        aVehicles.setOrganizationId(id);
        aVehicles.setStatus(0L);
        List<AVehicles> list = aVehiclesService.selectDispatchableAVehiclesList(aVehicles);
        return getDataTable(list);
    }

    /**
     * 查询对应部门可用的车辆列表
     */
    @RequiresPermissions("vehicles:vehicles:list")
    @GetMapping("/availableVehiclesList")
    public TableDataInfo availableVehiclesList(AVehicles aVehicles, HttpServletRequest request)
    {
        // 使用 TokenService 解析令牌,并判断令牌是否为空
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (loginUser == null) {
            return null;
        }
        //获取当前登录用户的部门信息，解析出部门ID和部门名称存储到车辆对象中去
        SysUser sysUser = loginUser.getSysUser();
        aVehicles.setOrganizationId(sysUser.getDeptId());
        startPage();
        List<AVehicles> list = aVehiclesService.selectAvailableAVehiclesList(aVehicles,sysUser.getDeptId());
        return getDataTable(list);
    }

    /**
     * 查询对应部门的车辆列表
     */
    @RequiresPermissions("vehicles:vehicles:list")
    @GetMapping("/list")
    public TableDataInfo list(AVehicles aVehicles, HttpServletRequest request)
    {
        // 使用 TokenService 解析令牌,并判断令牌是否为空
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (loginUser == null) {
            return null;
        }
        //获取当前登录用户的部门信息，解析出部门ID和部门名称存储到车辆对象中去
        SysUser sysUser = loginUser.getSysUser();
        aVehicles.setOrganizationId(sysUser.getDeptId());
        startPage();
        List<AVehicles> list = aVehiclesService.selectAVehiclesList(aVehicles);
        return getDataTable(list);
    }

    /**
     * 查询除了当前部门的车辆列表
     * @param aVehicles 车辆
     * @param request 请求
     * @return 车辆集合
     */
    @RequiresPermissions("vehicles:vehicles:listDept")
    @GetMapping("/listDept")
    public TableDataInfo listDept(AVehicles aVehicles, HttpServletRequest request)
    {
        // 使用 TokenService 解析令牌,并判断令牌是否为空
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (loginUser == null) {
            return null;
        }
        //获取当前登录用户信息
        SysUser sysUser = loginUser.getSysUser();
        startPage();
        List<AVehicles> list = aVehiclesService.selectAVehiclesListDept(aVehicles,sysUser);
        return getDataTable(list);
    }

    /**
     * 导出车辆列表
     */
    @RequiresPermissions("vehicles:vehicles:export")
    @Log(title = "车辆", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AVehicles aVehicles)
    {
        List<AVehicles> list = aVehiclesService.selectAVehiclesList(aVehicles);
        ExcelUtil<AVehicles> util = new ExcelUtil<AVehicles>(AVehicles.class);
        util.exportExcel(response, list, "车辆数据");
    }

    /**
     * 获取车辆详细信息
     */
    @RequiresPermissions("vehicles:vehicles:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(aVehiclesService.selectAVehiclesById(id));
    }

    /**
     * 新增车辆
     */
    @RequiresPermissions("vehicles:vehicles:add")
    @Log(title = "车辆", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(AddVehiclesForm form, HttpServletRequest request)
    {
        System.out.println(form);
        System.out.println(form.getFile());
        // 使用 TokenService 解析令牌,并判断令牌是否为空
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (loginUser == null) {
            return error("无效的令牌");
        }
        //获取当前登录用户的部门信息，解析出部门ID和部门名称存储到车辆对象中去
        SysUser sysUser = loginUser.getSysUser();
        AjaxResult ajax = toAjax(aVehiclesService.insertAVehicles(BeanUtil.toBean(form, AddVehiclesParam.class),sysUser));
        return ajax;
    }

    /**
     * 修改车辆
     */
    @RequiresPermissions("vehicles:vehicles:edit")
    @Log(title = "车辆", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UpdateVehiclesForm form)
    {
        return toAjax(aVehiclesService.updateAVehicles(BeanUtil.toBean(form, UpdateVehiclesParam.class)));
    }

    /**
     * 删除车辆
     */
    @RequiresPermissions("vehicles:vehicles:remove")
    @Log(title = "车辆", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aVehiclesService.deleteAVehiclesByIds(ids));
    }

    /**
     * 内部接口，修改车辆状态为使用中和修改状态为空闲
     * @param sysVehicles 车辆集合
     * @return 结果
     */
    @Log(title = "车辆", businessType = BusinessType.IMPORT)
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody Set<SysVehicles> sysVehicles)
    {
        AjaxResult ajax = toAjax(aVehiclesService.updateVehiclesStatus(sysVehicles));
        return ajax;
    }
    /**
     * 内部接口，获取车辆详情信息
     */
    @Log(title = "车辆", businessType = BusinessType.IMPORT)
    @PostMapping("/selectVehicles")
    public AjaxResult selectVehicles(@RequestBody Set<SysVehicles> sysVehicles)
    {
        AjaxResult ajax = success(aVehiclesService.selectVehicles(sysVehicles));
        return ajax;
    }
    /**
     * 内部接口，修改车辆实际单位
     */
    @Log(title = "车辆", businessType = BusinessType.IMPORT)
    @PostMapping("/updateUseUnit")
    AjaxResult updateUseUnit(@RequestBody UpdateUseUnitDTO updateUseUnitDTO){
        AjaxResult ajax = toAjax(aVehiclesService.updateVehiclesUseUnit(updateUseUnitDTO));
        return ajax;
    }

    /**
     * 内部接口，移除车辆实际单位
     */
    @Log(title = "车辆", businessType = BusinessType.IMPORT)
    @PostMapping("/removeUseUnit")
    AjaxResult removeUseUnit(@RequestBody Set<SysVehicles> sysVehicles){
        AjaxResult ajax = toAjax(aVehiclesService.removeUseUnit(sysVehicles));
        return ajax;
    }
}
